package com.atguigu.juc.day04.cf;

import com.atguigu.juc.day04.pool.Demo01MyPool;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class Demo12Exceptionally {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // exceptionally() 方法：如果前面任务没有抛异常，则不触发事件。
        // 如果前面任务抛异常，则触发事件，调用 exceptionally() 方法中通过 Function 对象设定的回调函数
        String finalTaskResult = CompletableFuture.supplyAsync(() -> "task 01 result", Demo01MyPool.getExecutorService())
                .thenApply(prevTaskResult -> {
                    System.out.println("prevTaskResult = " + prevTaskResult + " " + (10 / 0));
                    return "task 02 result";
                }).exceptionally(throwable -> {
                    System.out.println("throwable = " + throwable);
                    return "task result exceptionally";
                }).get();

        System.out.println("finalTaskResult = " + finalTaskResult);

    }

}
